<?php

/**
 * 优惠券操作封装
 */

namespace Logic;

class Coupon {

    /**
     * 清理领券地址
     */
    public static function cleanCouponUrl($coupon_url = '') {
        if (!$coupon_url) {
            return '';
        }

        $coupon_url = strpos($coupon_url, 'http') === false ? 'https:' . $coupon_url : $coupon_url;
        $t = parse_url($coupon_url);

        if (!isset($t['scheme']) || !isset($t['query']) || !isset($t['host']) || !isset($t['path'])) {
            return '';
        }

        $query = explode('&', $t['query'])[0];
        $return = $t['scheme'] . '://' . $t['host'] . $t['path'] . '?' . $query;
        return $return;
    }

    /**
     * 获取优惠券面值
     */
    public static function getAmount($coupon_info = '') {
        if (!$coupon_info) {
            return 0;
        }

        $cut = $coupon_info;
        $amount = 0;

        // 如果包含满减字样
        if (strpos($coupon_info, '减') !== false) {
            $cut = explode('减', $coupon_info)[1];
        }

        if (strpos($cut, '元') !== false) {
            // 优惠券面值
            $amount = explode('元', $cut)[0];
        }

        return $amount;
    }

}
